package org.ceteca.explica.client.gui.common;

import java.util.HashSet;

import org.ceteca.explica.client.gui.ExplicaGUIBuilder;
import org.ceteca.explica.core.InterfaceExplica;
import org.ceteca.explica.core.common.BaseVO;
import org.ceteca.explica.core.common.MultiSelectionSubentityOT;
import org.ceteca.explica.core.common.PriceListLineOT;
import org.ceteca.explica.core.common.PriceListOT;
import org.ceteca.explica.core.common.SelectionSubentityOT;
import org.ceteca.explica.core.util.Converter;
import org.ceteca.explica.core.util.OperationResult;
import org.ceteca.explica.core.warehouse.InterfaceWarehouse;
import org.ceteca.explica.core.warehouse.ProductOT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;

/**
 * @author Juan C. Díaz (juancarlosdiaz@ceteca.net) <br>
 *         Copyright (c) 2009 Fundación Centro Tecnolóxico da Carne
 * 
 * <br>
 * <br>
 *         This program is free software: you can redistribute it and/or modify
 *         it under the terms of the GNU General Public License as published by
 *         the Free Software Foundation, either version 3 of the License, or (at
 *         your option) any later version. <br/>
 *         This program is distributed in the hope that it will be useful, but
 *         WITHOUT ANY WARRANTY 
 *         <br>without even the implied warranty of
 *         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *         General Public License for more details. <br/>
 *         You should have received a copy of the GNU General Public License
 *         along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
public class PriceListLineClient extends CommonClient {

	protected Text entryPriceListLineId, entryPriceListLineProduct,
			entryPriceListLineDisccount, entryPriceListLinePrice,
			entryPriceListLinePriceList;

	protected Button buttonSelectProduct, buttonSelectPriceList;

	protected Button saveButton, cancelButton, undoButton;

	public PriceListLineClient() {
		super(	PriceListLineOT.class, 
				InterfaceExplica.EXPLICA_EDIT_PRICELISTLINE_GLADE_PATH,
				InterfaceExplica.EXPLICA_PRICELISTLINE_DIALOG_NAME);
	}

	@Override
	public void clearEntityData() {
		PriceListLineOT pll = new PriceListLineOT();
		if (isDetail()) {
			if (this.entityDetailView.getSubentityData().getParentEntity().getEntity().getKey()
					.equals(InterfaceExplica.EXPLICA_ENTITY_PRICELIST_ID)) {
				PriceListOT parentPriceList = (PriceListOT) entityDetailView
						.getSubentityData().getParentEntity()
						.getSelectedEntity();
				pll.setPriceList(parentPriceList);
				entryPriceListLinePriceList.setEnabled(false);
				buttonSelectPriceList.setEnabled(false);
			}
			
			if (this.entityDetailView.getSubentityData().getParentEntity().getEntity().getKey()
					.equals(InterfaceWarehouse.EXPLICA_ENTITY_PRODUCT_ID)) {
				ProductOT parentProduct = (ProductOT) entityDetailView
						.getSubentityData().getParentEntity().getSelectedEntity();
				pll.setProduct(parentProduct);
				entryPriceListLineProduct.setEnabled(false);
				buttonSelectProduct.setEnabled(false);
			}
		}

		setWidgetsValues(pll);
	}

	@Override
	public void buildSelectionSets(){
		lSelectionSubentities=new HashSet<SelectionSubentityOT>();
		lSelectionSubentities.add(new SelectionSubentityOT(InterfaceWarehouse.EXPLICA_ENTITY_PRODUCT_ID
				, InterfaceExplica.EDIT_PRICELISTLINE_PRODUCT_WIDGETNAME, this, buttonSelectProduct));
		lSelectionSubentities.add(new SelectionSubentityOT(InterfaceExplica.EXPLICA_ENTITY_PRICELIST_ID
				, InterfaceExplica.EDIT_PRICELISTLINE_PRICELIST_WIDGETNAME, this, buttonSelectPriceList));
		
		lMultiSelectionSubentities=new HashSet<MultiSelectionSubentityOT>();
		
	}
	
	@Override
	public BaseVO getEntityData() {
		PriceListLineOT entity = new PriceListLineOT();
		if (this.entEditView != null) {
			entity.setPrice(Converter.getInstance().parseBigDecimal(
					entryPriceListLinePrice.getText()));
			entity.setDisccount((Converter.getInstance()
					.parseFloat(entryPriceListLineDisccount.getText())));
			entity.setProduct((ProductOT) entryPriceListLineProduct.getData());
			entity.setPriceList((PriceListOT) entryPriceListLinePriceList
					.getData());

			if (selectedEntity != null) {
				entity.setVersion(((PriceListLineOT) this.selectedEntity).getVersion());
				entity.setIdPriceListLine(((PriceListLineOT) this.selectedEntity).getIdPriceListLine());
			}
		}

		return entity;
	}

	@Override
	public OperationResult validateEntity() {
		// TODO Auto-generated method stub
		return null;
	}



	@Override
	public void getWidgets() {
		boolean allOk = true;

		saveButton = (Button) this.entEditView.getGui().getWidget(
				InterfaceExplica.EXPLICA_ENTITYEDITION_SAVE_BUTTON_NAME);

		undoButton = (Button) this.entEditView.getGui().getWidget(
				InterfaceExplica.EXPLICA_ENTITYEDITION_UNDO_BUTTON_NAME);

		cancelButton = (Button) this.entEditView.getGui().getWidget(
				InterfaceExplica.EXPLICA_ENTITYEDITION_CANCEL_BUTTON_NAME);

		if ((entryPriceListLineId = ((Text) this.entEditView.getGui().getWidget(
						InterfaceExplica.EDIT_PRICELISLILNE_CODE_WIDGETNAME))) == null)	{
			allOk=false;
			fireExceptionAndExit("Error widget "+InterfaceExplica.EDIT_PRICELIST_CODE_WIDGETNAME);
		}

		if ((entryPriceListLineProduct = ((Text) this.entEditView.getGui().getWidget(
						InterfaceExplica.EDIT_PRICELISTLINE_PRODUCT_WIDGETNAME))) == null) {
			allOk=false;
			fireExceptionAndExit("Error widget "+InterfaceExplica.EDIT_PRICELISTLINE_PRODUCT_WIDGETNAME);
		}
		if ((entryPriceListLinePrice = (Text) this.entEditView.getGui().getWidget(
						InterfaceExplica.EDIT_PRICELISTLINE_PRICE_WIDGETNAME)) == null){
			allOk=false;
			fireExceptionAndExit("Error widget "+InterfaceExplica.EDIT_PRICELISTLINE_PRICE_WIDGETNAME);
		}
		if ((entryPriceListLineDisccount = (Text) this.entEditView.getGui().getWidget(
						InterfaceExplica.EDIT_PRICELISTLINE_DISCOUNT_WIDGETNAME)) == null) {
			allOk=false;
			fireExceptionAndExit("Error widget " + InterfaceExplica.EDIT_PRICELISTLINE_DISCOUNT_WIDGETNAME);
		}

		if ((entryPriceListLinePriceList = (Text) this.entEditView.getGui().getWidget(
						InterfaceExplica.EDIT_PRICELISTLINE_PRICELIST_WIDGETNAME)) == null) {
			allOk=false;
			fireExceptionAndExit("Error widget "+InterfaceExplica.EDIT_PRICELISTLINE_PRICELIST_WIDGETNAME);
		}

		if ((buttonSelectPriceList = (Button) this.entEditView.getGui().getWidget(
						InterfaceExplica.EDIT_PRICELISTLINE_SELECT_PRICELIST_BUTTON_WIDGETNAME)) == null) {
			allOk=false;
			fireExceptionAndExit("Error widget "+InterfaceExplica.EDIT_PRICELISTLINE_SELECT_PRICELIST_BUTTON_WIDGETNAME);
		}

		if ((buttonSelectProduct = (Button) this.entEditView.getGui().getWidget(
						InterfaceExplica.EDIT_PRICELISTLINE_SELECT_PRODUCT_BUTTON_WIDGETNAME)) == null) {
			allOk=false;
			fireExceptionAndExit("Error widget "+InterfaceExplica.EDIT_PRICELISTLINE_SELECT_PRODUCT_BUTTON_WIDGETNAME);
		}
	}

	@Override
	public void setEditionMode(boolean enabled) {
		entryPriceListLineDisccount.setEnabled(enabled);
		entryPriceListLineId.setEnabled(false);
		entryPriceListLinePrice.setEnabled(enabled);
		entryPriceListLinePriceList.setEnabled(enabled);
		entryPriceListLineProduct.setEnabled(enabled);
		buttonSelectPriceList.setEnabled(enabled);
		buttonSelectProduct.setEnabled(enabled);
		undoButton.setEnabled(enabled);
		cancelButton.setEnabled(enabled);
		saveButton.setEnabled(enabled);
		if (isDetail()) {
			if (this.entityDetailView.getSubentityData().getParentEntity().getEntity().getKey()
					.equals(InterfaceExplica.EXPLICA_ENTITY_PRICELIST_ID)) {
				entryPriceListLinePriceList.setEnabled(false);
				buttonSelectPriceList.setEnabled(false);
			}
			
			if (this.entityDetailView.getSubentityData().getParentEntity().getEntity().getKey()
					.equals(InterfaceWarehouse.EXPLICA_ENTITY_PRODUCT_ID)) {
				entryPriceListLineProduct.setEnabled(false);
				buttonSelectProduct.setEnabled(false);
			}
		}

	}

	public void setWidgetsValues(BaseVO entity) {
		PriceListLineOT ent = (PriceListLineOT) entity;
		entryPriceListLineId.setText(Converter.getInstance().toString(
				ent.getIdPriceListLine()));
		entryPriceListLinePrice.setText(Converter.getInstance().toString(
				ent.getPrice()));
		entryPriceListLineDisccount.setText(Converter.getInstance().toString(
				ent.getDisccount()));

		if (ent.getProduct() != null) {
			entryPriceListLineProduct
					.setText(ent.getProduct().getDescription());
			entryPriceListLineProduct.setData(ent.getProduct());
		}
		else{
			entryPriceListLineProduct.setText("");
			entryPriceListLineProduct.setData(null);
		}
		
		if (ent.getPriceList() != null) {
			entryPriceListLinePriceList.setText(ent.getPriceList()
					.getDescription());
			entryPriceListLinePriceList.setData(ent.getPriceList());
		}
		else{
			entryPriceListLinePriceList.setText("");
			entryPriceListLinePriceList.setData(null);
		}
	}
	
	@Override
	public void fillEntityData() {
		PriceListLineOT ent = (PriceListLineOT) this.getSelectedEntity();
		entryPriceListLineId.setText(Converter.getInstance().toString(
				ent.getIdPriceListLine()));
		entryPriceListLinePrice.setText(Converter.getInstance().toString(
				ent.getPrice()));
		entryPriceListLineDisccount.setText(Converter.getInstance().toString(
				ent.getDisccount()));

		if (ent.getProduct() != null) {
			entryPriceListLineProduct
					.setText(ent.getProduct().getDescription());
			entryPriceListLineProduct.setData(ent.getProduct());
		}
		else{
			entryPriceListLineProduct.setText("");
			entryPriceListLineProduct.setData(null);
		}
		
		if (ent.getPriceList() != null) {
			entryPriceListLinePriceList.setText(ent.getPriceList()
					.getDescription());
			entryPriceListLinePriceList.setData(ent.getPriceList());
		}
		else{
			entryPriceListLinePriceList.setText("");
			entryPriceListLinePriceList.setData(null);
		}
	}

	@Override
	public void init_specific_components() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void build_extension_fields_view() {
		// TODO Auto-generated method stub
		
	}
}
